package com.lin.cms.service.impl;

import java.util.List;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lin.cms.util.Paging;
import com.lin.cms.criteria.ChannelCriteria;
import com.lin.cms.domain.Channel;
import com.lin.cms.service.ChannelService;
import com.lin.cms.dao.ChannelMapper;

@Service
public class ChannelServiceImpl implements ChannelService{
	
	public Channel selectByPrimaryKey(Integer channelId){
		return channelMapper.selectByPrimaryKey(channelId);
	}
	
	public List<Channel> selectByCriteria(ChannelCriteria criteria){
		return channelMapper.selectByCriteria(criteria);
	}
	
	public int deleteByPrimaryKey(Integer channelId){
		int actualRowsAffected = 0;
		
		Channel _oldChannel = channelMapper.selectByPrimaryKey(channelId);
		
		if (_oldChannel != null) {
			actualRowsAffected = channelMapper.deleteByPrimaryKey(channelId);
		}
		
		return actualRowsAffected;
	}
	
	public int deleteByCriteria(ChannelCriteria criteria){
		int actualRowsAffected = 0;
		
		List<Channel> _oldChannel = channelMapper.selectByCriteria(criteria);
		
		if (_oldChannel.size() != 0) {
			actualRowsAffected = channelMapper.deleteByCriteria(criteria);
		}
		
		return actualRowsAffected;
	}
	
	public int insert(Channel channel){
		return channelMapper.insert(channel);
	}
	
	public int updateByPrimaryKeySelective(Channel channel){
		int actualRowsAffected = 0;
		
		int advertisingId = channel.getChannelId();

		Channel _oldChannel = channelMapper.selectByPrimaryKey(advertisingId);
		
		if (_oldChannel != null) {
			actualRowsAffected = channelMapper.updateByPrimaryKeySelective(channel);
		}
		
		return actualRowsAffected;
	}
	
	public int updateByPrimaryKey(Channel channel){
		int actualRowsAffected = 0;
		
		int advertisingId = channel.getChannelId();

		Channel _oldChannel = channelMapper.selectByPrimaryKey(advertisingId);
		
		if (_oldChannel != null) {
			actualRowsAffected = channelMapper.updateByPrimaryKey(channel);
		}
		
		return actualRowsAffected;
	}
	
	public List<Channel> selectByPage(ChannelCriteria criteria) {
		int totalResults = channelMapper.countByCriteria(criteria);
		Paging paging = criteria.getPaging();
		paging.setTotalResults(totalResults);
		
		return channelMapper.selectByPage(criteria,new RowBounds(paging.getFirstResult(),paging.getMaxResults()));
	}
	
	public int countByCriteria(ChannelCriteria criteria){
		return channelMapper.countByCriteria(criteria);
	}
	
	public List<Channel> getTopList(Integer siteId, boolean hasContentOnly) {
		// TODO Auto-generated method stub
		return channelMapper.getTopList(siteId, hasContentOnly, false);
	}
	
	public List<Channel> getChildList(Integer parentId, boolean hasContentOnly) {
		// TODO Auto-generated method stub
		return channelMapper.getChildList(parentId, hasContentOnly, false);
	}
	
	@Autowired
    private ChannelMapper channelMapper;

	
}
